package com.summer.peony.sso.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.ServiceException;
import com.summer.peony.sso.service.FileUpAndDownService;

@RestController
@RequestMapping("/upload")
public class FileUploadController {

	private static final Logger LOGGER = LoggerFactory.getLogger(FileUploadController.class);

	@Autowired
	private FileUpAndDownService fileUpAndDownService;

	@PostMapping(value = "/setFileUpload")
	public AjaxResult setFileUpload(@RequestParam(value = "file", required = false) MultipartFile file) {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			if (!file.isEmpty()) {
				ajaxResult = fileUpAndDownService.uploadPicture(file);
				return ajaxResult;
			} else {
				LOGGER.info(">>>>>>上传图片为空文件");
				ajaxResult.put("code", 900015);
				ajaxResult.put("msg", "上传图片为空文件");
				return ajaxResult;
			}
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOGGER.error(">>>>>>图片上传异常，e={}", e.getMessage());
			ajaxResult.put("code", 900014);
			ajaxResult.put("msg", "图片上传异常");
			return ajaxResult;
		}
	}
}